import Datetime from "vux/src/components/datetime/index.vue";
import dateFormat from "vux/src/tools/date/format.js";
import Checker from "vux/src/components/checker/checker.vue";
import CheckerItem from "vux/src/components/checker/checker-item.vue";
import Popup from "vux/src/components/popup/index.vue";

import { setTimeout } from "timers";
import { constants } from "zlib";

import template from "./lotteryResultTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  filters: {
    dateFormat,
  },
  components: {
    Datetime,
    Checker,
    CheckerItem,
    Popup,
  },
  data() {
    return {
      nowTime: "",
      nowTimeYMD: "YYYY-MM-DD",
      nowYear: dateFormat(new Date(), "YYYY"),
      minYear: "2000",
      maxYear: "2030",

      resultData: [],

      pullUpLoad: true,
      pullDownRefresh: true,
      page: 1,

      lotteryList: [],
      lotteryValue: [],

      lotteryData: [],
      checkerData: "",

      lotteryFlag: false,
      lottery_type: null,

      ballLhcRedCls:
        "1, 2, 7, 8, 12, 13, 18, 19, 23, 24, 29, 30, 34, 35, 40, 45, 46",
      ballLhcBlueCls:
        "3, 4, 9, 10, 14, 15, 20, 25, 26, 31, 36, 37, 41, 42, 47, 48",
      ballLhcGreenCls:
        "5, 6, 11, 16, 17, 21, 22, 27, 28, 32, 33, 38, 39, 43, 44, 49",

      roomInfo: {},
    };
  },

  created() {
    if (localStorage.getItem("lotteryId")) {
      this.lottery_type = localStorage.getItem("lotteryId");
    } else {
      this.lottery_type = this.$route.query.lottery_type;
    }
  },
  mounted() {
    if (this.lottery_type == 7) {
      this.nowTime = "";
      setTimeout(() => {
        this.nowTime = dateFormat(new Date(), "YYYY");
        this.nowTimeYMD = "YYYY";
        this.minYear = Number(this.nowYear) - 2;
        this.maxYear = Number(this.nowYear);
      }, 20);
    }
  },
  computed: {},
  methods: {
    goLotteryRoom() {
      //去彩种房间
      if (!localStorage.getItem("token")) {
        var that = this;
        this.$vux.alert.show({
          content: "您还未登录，请先登录",
          onHide() {
            that.$router.push({
              path: "/login",
            });
          },
        });
        return;
      }
      var params = {
        token: localStorage.getItem("token"),
        lottery_type: this.lottery_type,
      };
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=openAward&a=instantlyBetting", params)
        .then((res) => {
          this.$vux.loading.hide();
          if (res.status == 0) {
            this.roomInfo = res.data;
            console.log(this.roomInfo);

            this.$router.push({
              path: "/room/" + this.roomInfo.room_id,
              query: {
                lottery_type: this.roomInfo.lottery_type,
                id: this.roomInfo.room_id,
              },
            });
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
    lotteryTypeToggle: function () {
      //彩种列表开关
      if (this.lotteryFlag) {
        this.lotteryFlag = false;
      } else {
        this.lotteryFlag = true;
      }
    },
    onItemClick(value) {
      if (!this.value) {
        this.lotteryFlag = false;
      }
      this.lottery_type = value.id;
      localStorage.setItem("lotteryId", value.id);
    },
    headBack() {
      localStorage.removeItem("lotteryId");

      this.goLotteryRoom();
    },
    specificPoke(name) {
      var s;

      var nameArr = name.replace(/(\d+|[ajqk])/i, "-$1").split("-");
      if (nameArr[0] == "黑桃") {
        s = 1;
      }
      if (nameArr[0] == "红心") {
        s = 2;
      }
      if (nameArr[0] == "梅花") {
        s = 3;
      }
      if (nameArr[0] == "方块") {
        s = 4;
      }
      return "poker" + nameArr[1] + "_" + s;
    },
    pullingDown() {
      this.page = 1;
      this._getDataList(0);
    },
    pullingUp() {
      if (!this.resultData) {
        return;
      }
      if (this.pullUpLoadFlag) {
        this.$refs.scroll.forceUpdate(false);
        return false;
      }
      this.page += 1;
      this._getDataList(0);
    },
    _getDataList(flag) {
      //获取彩种数据列表
      var params = {
        token: localStorage.getItem("token"),
        date: this.nowTime,
        lottery_type: this.lottery_type,
        page: this.page,
      };
      if (flag) {
        this.$vux.loading.show();
      }
      this.pullUpLoadFlag = false;
      this.$http
        .post(this.urlRequest + "?m=api&c=openAward&a=dataList", params)
        .then((res) => {
          if (flag) {
            this.$vux.loading.hide();
          }
          console.log(res);
          if (res.status == 0) {
            if (this.page == 1) {
              this.resultData = res.list;
            } else {
              this.resultData = this.resultData.concat(res.list);
            }
            this.lotteryData = res.gameInfo;

            for (var key in this.lotteryData) {
              if (this.lotteryData[key].id == this.lottery_type) {
                this.checkerData = this.lotteryData[key].name;
                break;
              }
            }

            if (res.list.length < 50) {
              this.pullUpLoadFlag = true;
            }
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$refs.scroll.forceUpdate(false);
          this.pullUpLoad = false;
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
  },
  watch: {
    lottery_type(val) {
      if (val) {
        this.resultData = [];
        this.page = 1;
        if (val == 7) {
          this.nowTime = "";
          setTimeout(() => {
            this.nowTime = dateFormat(new Date(), "YYYY");
            this.nowTimeYMD = "YYYY";
            this.minYear = Number(this.nowYear) - 2;
            this.maxYear = Number(this.nowYear);
            this._getDataList(1);
          }, 20);
        } else {
          this.nowTime = "";
          setTimeout(() => {
            this.nowTime = dateFormat(new Date(), "YYYY-MM-DD");
            this.nowTimeYMD = "YYYY-MM-DD";
            this.minYear = 2000;
            this.maxYear = 2030;
            this._getDataList(1);
          }, 20);
        }
      }
    },
    nowTime(val) {
      if (val) {
        this.resultData = [];
        this.page = 1;
        this._getDataList(1);
        if (this.resultData.length) {
          this.$refs.scroll.scrollTo(0, 0);
        }
      }
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/room/lotteryResult/lotteryResult.vue
